package com.hanbang.chu_xing_gao_zhao.utils;

import android.content.Context;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.widget.Toast;

import com.hanbang.chu_xing_gao_zhao.application.Global;
import com.hanbang.chu_xing_gao_zhao.application.HttpInterfaces;
import com.hanbang.chu_xing_gao_zhao.application.MyApplication;
import com.hanbang.chu_xing_gao_zhao.entity.SharedData;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.client.HttpRequest;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;
import cn.sharesdk.sina.weibo.SinaWeibo;

/**
 * 分享的方法
 * Created by yang on 2016/2/16.
 */
public class SharedUtil {


    /**
     * @param context
     * @param sharedData
     */
    public static void shered(final Context context, final SharedData sharedData) {


        sharedData.setTitle(sharedData.getTitle() == null ? "暂无标题" : sharedData.getTitle());
        sharedData.setText(sharedData.getText() == null ? "暂无内容" : sharedData.getText());
        sharedData.setClickUrl(sharedData.getClickUrl() == null ? "www.hbung.com" : sharedData.getClickUrl());
        sharedData.setImageUrl(sharedData.getImageUrl() == null ? "" : sharedData.getImageUrl());


        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        // 关闭sso授权
        oks.disableSSOWhenAuthorize();
        // 参考代码配置章节，设置分享参数
        // 通过OneKeyShareCallback来修改不同平台分享的内容
        oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {

            @Override
            public void onShare(Platform platform, Platform.ShareParams paramsToShare) {
                switch (platform.getName()) {
                    case "QQ":
                    case "QZone":
                        paramsToShare.setTitle(sharedData.getTitle());
                        paramsToShare.setText(sharedData.getText());
                        paramsToShare.setTitleUrl(sharedData.getClickUrl());
                        paramsToShare.setImageUrl(sharedData.getImageUrl());

                        paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                        break;
                    case "Wechat":
                    case "WechatFavorite":
                    case "WechatMoments":
                        paramsToShare.setTitle(sharedData.getTitle());
                        paramsToShare.setText(sharedData.getText());
                        paramsToShare.setImageUrl(sharedData.getImageUrl());
                        paramsToShare.setUrl(sharedData.getClickUrl());
                        paramsToShare.setShareType(Platform.SHARE_WEBPAGE);
                        break;
                    default:
                        break;

                    case "SinaWeibo":
                        paramsToShare.setText(sharedData.getTitle());
                        paramsToShare.setImageUrl(sharedData.getImageUrl());
//                        paramsToShare.setUrl(clickUrlF);
                        paramsToShare.setShareType(Platform.SHARE_WEBPAGE);

                        break;
                }

            }
        });

        oks.setCallback(new PlatformActionListener() {

            @Override
            public void onError(Platform arg0, int arg1, Throwable arg2) {
                MyLog.e("PlatformActionListener", "onError   " + arg1);

            }

            @Override
            public void onComplete(Platform arg0, int arg1,
                                   HashMap<String, Object> arg2) {

                MyLog.e("PlatformActionListener", "onComplete   " + arg1
                        + "    " + arg2.toString());
                MyToast.show(context, "分享成功", 1);

//                MyLog.i("text", "*****************" + sharedData.getParams().toString());


                if (sharedData.getParams() != null) {

                    ((MyApplication) context.getApplicationContext()).httpUtils.send(HttpRequest.HttpMethod.POST, HttpInterfaces.Feed_Share, sharedData.getParams(), null);
                    //发送成功广播
                    Intent intent = new Intent();
                    intent.setAction(Global.DYNAMIC_SHARED);
                    context.sendBroadcast(intent);

                }


            }

            @Override
            public void onCancel(Platform arg0, int arg1) {
                MyLog.e("PlatformActionListener", "onCancel   " + arg1);
            }
        });
        oks.show(context);
    }
}
